import 'package:dio/dio.dart';
import 'package:ost_flutter_module/class/entity/base_entity/base_entity.dart';
import 'package:ost_flutter_module/class/entity/class_entity/login_entity.dart';
import 'package:ost_flutter_module/help/event_bus/event.dart';
import 'package:ost_flutter_module/help/event_bus/event_bus_manager.dart';
import 'package:ost_flutter_module/help/global/global_cache.dart';

import 'http_manager.dart';
import 'url_constants.dart';

class TokenInterceptor extends Interceptor {
  final Dio dio;

  TokenInterceptor(this.dio);

  @override
  Future onResponse(Response response) async {
    var entity = BaseEntity<dynamic>.fromJson(response.data);
    if (entity.code == 4000) {
      // token失效. 续token, 如果续失败则退出登录
      // var globalCache = GlobalCache.getInstance();
      // var params = {
      //   "refresh_token": globalCache.getLoginEntity().refreshToken,
      //   "grant_type": "refresh_token"
      // };
      // var result = await HttpManager.getInstance().syncRequest<LoginEntity>(
      //   URL_LOGIN,
      //   params: params,
      //   contentType: Headers.formUrlEncodedContentType,
      // );
      // if (result.isSuccess) {
      //   GlobalCache.getInstance().saveLoginEntity(result.getOrNull);
      //   var options = response.request;
      //   return dio.request(options.path, options: options);
      // } else {
      //   EventBusManager.getInstance().postEvents(LoginOutEvent());
      // }
    }
    return super.onResponse(response);
  }
}
